#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<unistd.h>
#include<omp.h>
#include"defs.h"

int main(int argc, char **argv){
	int rank, size;
	int vector_size = VECTOR_SIZE;
	int k;
	real piecesize;
	real *vec;	
	real sum=0.0;
	time_t timer1, timer2;
	char *omp_size_env;

	omp_size_env = getenv("OMP_NUM_THREADS");
	if (omp_size_env!=NULL){
		size = atoi(omp_size_env);
	}else{
		size = omp_get_num_procs();
	}

	timer1 = time(NULL);

	if (argc>1){
		vector_size = atoi(argv[1]);
	}
	vec = (real*)malloc(sizeof(real)*vector_size);

	if (argc>2){
		init_vector(vec,vector_size,atoi(argv[2]));
	}else{	
		init_vector(vec,vector_size,1.0);
	}

	#pragma omp parallel private(rank) shared(size) reduction(+:sum)
 	{
		size = omp_get_num_threads();
		#pragma omp for 
		for (k=0;k<vector_size;k++){
			rank = omp_get_thread_num();
			sum += exp2(vec[k]);
		}
		printf("(%d,%d) Calculando soma dos quadrados do vector da posicao = %.3llf\n", rank, size, sum);
	}
	timer2 = time(NULL);
	printf("Resultado final da soma: %.3llf\n", sum);
	printf("Resultado final da norma: %.3llf\n", sqrt(sum));

	free(vec);
	return 0;	
}
